package com.my.study.config.hook.application;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * <p>容器刷新完成扩展点</p>
 *
 * 容器刷新成功意味着所有的Bean初始化已经完成，当容器刷新之后Spring将会调用容器内所有实现了ApplicationListener<ContextRefreshedEvent>的Bean的onApplicationEvent方法
 *
 * @author: yidujun
 * @create: 2021/04/02 13:55
 */
@Slf4j
@Component
public class StartupApplicationListenerExample implements ApplicationListener<ContextRefreshedEvent> {

    private static int counter;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
//        if (event.getApplicationContext().getParent() == null) {
//            log.info("Increment counte counter={}", counter++);
//        }
        log.info("StartupApplicationListenerExample -> Increment counte counter={}", counter++);
    }
}
